package com.jeeplus.test.jlnu.tableManage.service.impl;


import com.jeeplus.test.jlnu.tableManage.domain.entity.JlnuTableManage;
import com.jeeplus.test.jlnu.tableManage.domain.query.TableManageQuery;
import com.jeeplus.test.jlnu.tableManage.mapper.JlnuTableManageMapper;
import com.jeeplus.test.jlnu.tableManage.service.JlnuTableManageService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 动态表头(JlnuTableManage)表服务实现类
 *
 * @author ChiHongYu
 * @since 2025-02-17 15:16:39
 */
@Service("jlnuTableManage")
public class JlnuTableManageServiceImpl implements JlnuTableManageService {
    @Resource
    private JlnuTableManageMapper jlnuTableManageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JlnuTableManage queryById(Integer id) {
        return this.jlnuTableManageDao.queryById(id);
    }

    @Override
    public JlnuTableManage getByOffice(String officeId, String type) {
        List<String> specificTypes = Arrays.asList("1", "2", "3", "4");

        JlnuTableManage jlnuTableManage;
        if (specificTypes.contains(type)) {
            // 如果 type 是 1、2、3、4，则按 officeId 查询
            jlnuTableManage = jlnuTableManageDao.getByOffice(officeId, type);
            if (jlnuTableManage == null) {
                jlnuTableManage = jlnuTableManageDao.getByOffice(null, type);
                jlnuTableManage.setId(null);
            }
        } else {
            // 如果 type 不是 1、2、3、4，只按 type 查询
            jlnuTableManage = jlnuTableManageDao.getByTypeOnly(type);
            jlnuTableManage.setContent(jlnuTableManage.getContent().replace("\r\n", ""));
        }
        return jlnuTableManage;
    }


    @Override
    public List<JlnuTableManage> queryByLimit(Map<String, Object> param, int limit1, int limit2) {
        return this.jlnuTableManageDao.page(param, limit1, limit2);
    }

    @Override
    public int queryCount(Map<String, Object> param) {
        return this.jlnuTableManageDao.count(param);
    }

    /**
     * 新增数据
     *
     * @param param 实例对象
     * @return 是否成功
     */
    @Override
    public boolean insertOrUpdate(TableManageQuery param) {
        List<String> specificTypes = Arrays.asList("1", "2", "3", "4");
        if (param.getId() != null) {
            JlnuTableManage tableManage = jlnuTableManageDao.queryById(param.getId());
            if (tableManage != null) {
                if (specificTypes.contains(tableManage.getType())) {
                    if (tableManage.getOfficeId() == null)
                        param.setId(null);
                    else
                        return jlnuTableManageDao.update(param.makeUpdateEntity(tableManage));

                } else {
                    return jlnuTableManageDao.update(param.makeUpdateEntity(tableManage));
                }


            }
        }
        return jlnuTableManageDao.insert(param.makeCreateEntity());
    }

    /**
     * 修改数据
     *
     * @param jlnuTableManage 实例对象
     * @return 是否成功
     */
    @Override
    public boolean update(JlnuTableManage jlnuTableManage) {
        return this.jlnuTableManageDao.update(jlnuTableManage);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.jlnuTableManageDao.deleteById(id);
    }

    /**
     * 通过主键修改使用状态
     *
     * @param id       主键
     * @param useState 使用状态
     * @return 是否成功
     */
    @Override
    public boolean changeUseById(Integer id, String useState) {
        return this.jlnuTableManageDao.changeUseById(id, useState) > 0;
    }
}
